package com.example.administrator.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.example.administrator.adapter.DialogFolderAdapter;
import com.example.administrator.bean.AlbumFolder;
import com.example.administrator.impl.OnCompatItemClickListener;
import com.example.administrator.mypictureshow.AlbumActivity;
import com.example.administrator.mypictureshow.R;

import java.util.List;

/**
 * 相册分类选择Dialog，全屏展示
 * Created by Administrator on 2018\8\1 0001.
 */

public class PhotoAlbumDialog extends Dialog {

    private Context mContext;
    private int checkPosition = 0;
    private DialogFolderAdapter folderAdapter;
    private List<AlbumFolder> albumFolders;
    private OnCompatItemClickListener mItemClickListener;

    public void setDialogData(List<AlbumFolder> albumFolders,OnCompatItemClickListener mItemClickListener) {
        this.albumFolders = albumFolders;
        this.mItemClickListener = mItemClickListener;
    }

    public PhotoAlbumDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        View view_content = LayoutInflater.from(mContext).inflate(R.layout.dialog_photo_album,null);
        setContentView(view_content);
        setDialogSize();
        //进行数据设置
        RecyclerView rvContentList = (RecyclerView) view_content.findViewById(R.id.rv_content_list);
        rvContentList.setHasFixedSize(true);
        rvContentList.setLayoutManager(new LinearLayoutManager(getContext()));
        folderAdapter = new DialogFolderAdapter(albumFolders, new OnCompatItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(null != mItemClickListener){
                    checkPosition = position;
                    mItemClickListener.onItemClick(view, position);
                }
                dismiss();
            }
        });
        rvContentList.setAdapter(folderAdapter);


    }

    private void setDialogSize(){
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = getScreenWidth();
        layoutParams.height = getScreenH(mContext) - getStatusBarHeight(mContext);
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);
        window.setWindowAnimations(R.style.bottomDialogAnim);
    }

    private int getScreenWidth() {
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * 获取高度
     * @param context
     * @return
     */
    public int getScreenH(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.heightPixels;
        return screenWidth;
    }

    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
